package smallfrench.ui;

import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextPane;
import javax.swing.JViewport;

@SuppressWarnings("serial")
public class MTextPane extends JTextPane implements Editable {

	private JViewport viewport;

	@SuppressWarnings("unused")
	private MDocument document;

	private SurligneurLigne surligneurLigne;

	//---------------------------------------------------------

	public MTextPane(MDocument document) {
		setStyledDocument(document);
		this.document = document;

		surligneurLigne = new SurligneurLigne(this);

		addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {}

			@Override
			public void focusGained(FocusEvent e) {
				Actions.setSelectionnableActif(MTextPane.this);
			}
		});
	}

	//---------------------------------------------------------

	public void setViewport(JViewport viewport) {
		this.viewport = viewport;
	}

	public void setCouleurLigneErreur() {
		surligneurLigne.setCouleurErreur();
	}

	@Override
	public Dimension getPreferredSize() {
		Dimension pf = super.getPreferredSize();

		return new Dimension(
				Math.max(pf.width, viewport.getWidth()),
				Math.max(pf.height, viewport.getHeight()));
	}

	@Override
	public boolean getScrollableTracksViewportWidth() {
		return false;
	}

	@Override
	public boolean getScrollableTracksViewportHeight() {
		return false;
	}

	@Override
	public void copier() {
		copy();
	}

	@Override
	public void toutSelectionner() {
		selectAll();
	}

	@Override
	public void couper() {
		cut();
	}

	@Override
	public void coller() {
		paste();
	}

}
